{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二叉树中2个节点的最近共同祖先\n",
    "\n",
    "&emsp;&emsp;给定一棵二叉树和树上两个互不相同的节点，查找并返回两个节点最近的共同祖先。\n",
    "\n",
    "&emsp;&emsp;对于这个问题，你可以假设每个节点都有一个指向其父节点、左子节点、右子节点的指针。\n",
    "\n",
    "```text\n",
    " In[1]: A = {'value': 'd', 'parent': 'c', 'left': None, 'right': None}\n",
    "        B = {'value': 'e', 'parent': 'c', 'left': None, 'right': None}\n",
    "        tree = Tree(\n",
    "            '  a      \\n' +\n",
    "            ' / \\     \\n' +\n",
    "            'b  (c)   \\n' +\n",
    "            '   / \\   \\n' +\n",
    "            '  d*  e* \\n'\n",
    "        )\n",
    "\n",
    "Out[1]: 'c'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TreeNode:\n",
    "    def __init__(self, value):\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "        self.parent = None\n",
    "        self.value = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lowestCommonAncestor(root, a, b):\n",
    "    def depth(node):\n",
    "        cnt = 0\n",
    "        while node:\n",
    "            cnt += 1\n",
    "            node = node.parent\n",
    "        return cnt\n",
    "\n",
    "    depthA = depth(a)\n",
    "    depthB = depth(b)\n",
    "    if depthA < depthB:\n",
    "        while depthA < depthB:\n",
    "            b = b.parent\n",
    "            depthB -= 1\n",
    "    else:\n",
    "        while depthA > depthB:\n",
    "            a = a.parent\n",
    "            depthA -= 1\n",
    "    while a and b and a != b:\n",
    "        a = a.parent\n",
    "        b = b.parent\n",
    "    return a if a == b else None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = TreeNode('a')\n",
    "root.left = TreeNode('b')\n",
    "root.left.parent = root\n",
    "root.right = TreeNode('c')\n",
    "root.right.parent = root\n",
    "a = root.right.left = TreeNode('d')\n",
    "root.right.left.parent = root.right\n",
    "b = root.right.right = TreeNode('e')\n",
    "root.right.right.parent = root.right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n"
     ]
    }
   ],
   "source": [
    "print(lowestCommonAncestor(root, a, b).value)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
